package com.neusoft.neuiotms.common.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.common.dto.SerialNumberDTO;
import com.neusoft.neuiotms.common.model.SerialNumber;
import com.neusoft.neuiotms.common.repository.SerialNumberRepository;

@Component
public class SerialNumberConverter extends AbstractConverter<SerialNumber, SerialNumberDTO> {

    @Autowired
    private SerialNumberRepository serialNumberRepository;

    @Override
    public SerialNumber toModel(final SerialNumberDTO dto) {
        SerialNumber model;
        if (dto.isNew()) {
            model = new SerialNumber();
        } else {
            model = this.serialNumberRepository.getOne(dto.getId());
        }
        model.setConfigTemplet(dto.getConfigTemplet());
        model.setAutoIncrement(dto.getAutoIncrement());
        model.setMaxSerial(dto.getMaxSerial());
        model.setModuleCode(dto.getModuleCode());
        model.setModuleName(dto.getModuleName());
        model.setPreMaxNum(dto.getPreMaxNum());
        return model;
    }

    @Override
    public SerialNumberDTO toDTO(final SerialNumber model, final boolean forListView) {
        final SerialNumberDTO dto = new SerialNumberDTO();
        dto.setId(model.getId());
        dto.setAutoIncrement(model.getAutoIncrement());
        dto.setAutoIncrementDesc(model.getAutoIncrement() ? "是" : "否");
        dto.setMaxSerial(model.getMaxSerial());
        if (model.getMaxSerial() == null) {
            dto.setMaxSerialDesc("0");
        } else {
            dto.setMaxSerialDesc(model.getMaxSerial().toString());
        }
        dto.setModuleCode(model.getModuleCode());
        dto.setModuleName(model.getModuleName());
        dto.setPreMaxNum(model.getPreMaxNum());
        dto.setConfigTemplet(model.getConfigTemplet());
        return dto;
    }

}
